package com.oyb.dao;

import com.oyb.entity.Admin;
import com.oyb.util.ConnectionUtils;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class AdminDao {
//添加
    public int insertAdmin(Admin admin){
        String sql = "insert into  admin value (?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement ps = null;
        Connection connection = null;
        try {
            //提前处理权限
            List<String> powers = admin.getPower();
            String powerStr = "";
            for (String power : powers) {
                if (powerStr.length() != 0) powerStr += "-";
                powerStr += power;
            }

            connection = ConnectionUtils.getconnection();
            ps = connection.prepareStatement(sql);

            ps.setObject(1,admin.getId());
            ps.setObject(2,admin.getUsername());
            ps.setObject(3,admin.getPassword());
            ps.setObject(4,admin.getSex());
            //权限
            ps.setObject(5,powerStr);
            ps.setObject(6,admin.getTruename());
            ps.setObject(7,admin.getBirthday());
            ps.setObject(8,admin.getHomepalce());
            ps.setObject(9,admin.getJiguan());
            ps.setObject(10,admin.getPhoto());

            int result = ps.executeUpdate();
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnectionUtils.close(ps,connection,null);
        }
        return 0;
    }

//查询
    public Admin queryadmin(String uname,String pword){
        String sql = "select * from admin where username = ? and password = ?";
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            connection = ConnectionUtils.getconnection();
            ps = connection.prepareStatement(sql);
            ps.setString(1,uname);
            ps.setString(2,pword);
            resultSet = ps.executeQuery();

            //把查询到的信息实例化对象返回给登录servlet，让其存到session
            Admin admin = new Admin();
            //resultSet.next()出现一次就算一次
            if(resultSet.next()){
                admin.setId(resultSet.getInt("id"));
               admin.setUsername(resultSet.getString("username"));
               admin.setPassword(resultSet.getString("password"));
               admin.setBirthday(resultSet.getTimestamp("birthday"));
                admin.setSex(resultSet.getString("sex"));
                admin.setTruename(resultSet.getString("truename"));
                admin.setHomepalce(resultSet.getString("homepalce"));
                admin.setJiguan(resultSet.getString("jiguan"));
                admin.setPhoto(resultSet.getString("photo"));

                //反向处理权限
                //1-2-3 -> List<String>
                String power = resultSet.getString("power");
                List<String> powerList = Arrays.asList(power.split("-"));
                admin.setPower(powerList);
            }
            return admin;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionUtils.close(ps,connection,resultSet);
        }
        return null;
    }

//    private Integer id;
//    private String username;
//    private String password;
//    private List<String> power;
//    private Date birthday;

//修改
    public int upAdmin(Admin admin){
        Connection connection = ConnectionUtils.getconnection();
        String sql = "update admin set username=?,password=?,power=?,birthday=? where id=?";
        PreparedStatement ps=null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setObject(1,admin.getUsername());
            ps.setObject(2,admin.getPassword());
            ps.setObject(3,admin.getPowerStr());
            ps.setObject(4,new SimpleDateFormat("yyyy-MM-dd").format(admin.getBirthday()));
            ps.setObject(5,admin.getId());
            return ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnectionUtils.close(ps,connection,null);
        }
        return 0;
    }
}
